package oj;

import java.util.LinkedList;
import java.util.Queue;

public class Num958 {
    public boolean isCompleteTree(TreeNode root) {
        if(root==null || root.left==null&&root.right==null){
            return true;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        boolean isSecond=false;
        while (!queue.isEmpty()){
            TreeNode cur=queue.poll();
            if(!isSecond){
                if(cur.left!=null&&cur.right!=null){
                    queue.offer(cur.left);
                    queue.offer(cur.right);
                }else if(cur.left==null&&cur.right!=null){
                    return false;
                }
                else if(cur.left!=null){
                    queue.offer(cur.left);
                    isSecond=true;
                } else{
                    isSecond=true;
                }
            }else {
                if(cur.left!=null || cur.right!=null){
                    return false;
                }
            }
        }
        return true;
    }
}
